local shenyi = fk.CreateSkill{
    name = "ym2__shenyi",
}
Fk:loadTranslationTable{
    ["ym2__shenyi"] = "申义",
    [":ym2__shenyi"] = "出牌阶段限一次，你可以指定一名角色，连续选择与其拼点或【决斗】直到有角色连续获胜。每次赢后，你令其获得你本次失去的牌并明置，此后其每失去其中一张便翻面。",
    ["@@ym2__shenyi-inhand"] = "申义",
}
local DIY = require "packages/diy_utility/diy_utility"
shenyi:addEffect("active",{
    can_use = function (self, player)
        return player:usedSkillTimes(shenyi.name,Player.HistoryPhase) < 1
    end,
    card_filter = Util.FalseFunc,
    card_num = 0,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        local c = Fk:cloneCard("duel")
        c.skillName = shenyi.name
        return #selected == 0 and to_select ~= player and (player:canPindian(to_select) or player:canUseTo(c,to_select))
    end,
    target_num = 1,
    on_use = function (self, room, effect)
        local player = effect.from
        local target = effect.tos[1]
        local winner = nil
        while true do
            if not player:isAlive() then return end
            local temp_winner = nil
            local choices = {}
            if player:canPindian(target) then
                table.insert(choices,"Pindian")
            end
            local card = Fk:cloneCard("duel")
            card.skillName = shenyi.name
            if player:canUseTo(card,target) then
                table.insert(choices,"duel")
            end
            if #choices > 0 then
                local choice = room:askToChoice(player,{
                    choices = choices,
                    skill_name = shenyi.name,
                    all_choices = {"Pindian","duel"},
                    cancelable = false,
                })
                room:setPlayerMark(player,"ym2__shenyi-process",1)
                if choice == "Pindian" then
                    local pindian = player:pindian({target}, shenyi.name)
                    if pindian.results[target].winner == player then
                        temp_winner = player
                        local cards = player:getTableMark("ym2__shenyi-phase")
                        if #cards > 0 then
                            room:obtainCard(target,cards,true,fk.ReasonJustMove,player,shenyi.name,"@@ym2__shenyi-inhand")
                            DIY.showCards(target, cards)
                        end
                    elseif pindian.results[target].winner == target then
                        temp_winner = target
                    end
                else
                    local now = room.logic:getCurrentEvent()
                    local use = room:useVirtualCard("duel",nil,player,{target},shenyi.name)
                    if use and #room.logic:getEventsByRule(GameEvent.Damage, 1, function (d_event)
                        local data = d_event.data
                        local c_event = d_event:findParent(GameEvent.CardEffect, false, 2)
                        if c_event == nil then return false end
                        local c_effect = c_event.data
                        return data.card and data.card == c_effect.card and data.card == use.card and data.to == player
                    end, now.id) == 1 then
                        temp_winner = target
                    else
                        temp_winner = player
                        local cards = player:getTableMark("ym2__shenyi-phase")
                        if #cards > 0 then
                            room:obtainCard(target,cards,true,fk.ReasonJustMove,player,shenyi.name,"@@ym2__shenyi-inhand")
                            DIY.showCards(target, cards)
                        end
                    end
                end
                room:setPlayerMark(player,"ym2__shenyi-process",0)
                room:setPlayerMark(player,"ym2__shenyi-phase",0)
            else
                break
            end
            if temp_winner and temp_winner == winner then
                break
            else
                winner = temp_winner
            end
        end
    end,
})
shenyi:addEffect(fk.AfterCardsMove,{
    global = true,
    can_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            if move.from == player and move.extra_data and move.extra_data.ym2__shenyi then
                return true
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            if move.from == player and move.extra_data and move.extra_data.ym2__shenyi then
                local x = move.extra_data.ym2__shenyi
                for i = 1, x, 1 do
                    player:turnOver()
                end
            end
        end
    end,
})
shenyi:addEffect(fk.BeforeCardsMove,{
    global = true,
    can_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            if move.from == player then
                for _, info in ipairs(move.moveInfo) do
                    if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and Fk:getCardById(info.cardId):getMark("@@ym2__shenyi-inhand") > 0 then
                        return true
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        for _, move in ipairs(data) do
            if move.from == player then
                local x = 0
                for _, info in ipairs(move.moveInfo) do
                    if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and Fk:getCardById(info.cardId):getMark("@@ym2__shenyi-inhand") > 0 then
                        x = x + 1
                    end
                end
                move.extra_data = move.extra_data or {}
                move.extra_data.ym2__shenyi = x
            end
        end
    end,
})
shenyi:addEffect(fk.AfterCardsMove,{
    can_refresh = function (self, event, target, player, data)
        if player:getMark("ym2__shenyi-process") ~= 0 then
            for _, move in ipairs(data) do
                if move.from == player then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("ym2__shenyi-phase")
        for _, move in ipairs(data) do
            if move.from == player then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                        table.insertIfNeed(mark,info.cardId)
                    end
                end
            end
        end
        room:setPlayerMark(player,"ym2__shenyi-phase",mark)
    end,
})
return shenyi